<?php
namespace Biz\model\designerAward;

use Doraemon\pockets\datebase\BaseDbModel;
use Doraemon\tools\ImgUrl;

class User extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_designer_award';

    const _CONFIG_SELECT_ = 'designer_award';

    const _DATABASE_ = 'biz_designer_award';

    const _TABLE_NAME = 'designer_award_user';

    //用户类别1业主2设计师3机构
    const USER_TYPE_OWNER = 1;
    const USER_TYPE_DESIGNER = 2;
    const USER_TYPE_COMPANY = 3;
    const USER_TYPE = [
        self::USER_TYPE_OWNER => '业主',
        self::USER_TYPE_DESIGNER => '设计师',
        self::USER_TYPE_COMPANY => '机构',
    ];

    //赛区 1华东 2西南 3华南 4华北
    const USER_ARAE_EAST = 1;
    const USER_ARAE_WEST = 2;
    const USER_ARAE_SOUTH = 3;
    const USER_ARAE_NORTH = 4;
    const USER_ARAE = [
        self::USER_ARAE_EAST => '华东',
        self::USER_ARAE_WEST => '西南',
        self::USER_ARAE_SOUTH => '华南',
        self::USER_ARAE_NORTH => '华北',
    ];

    public static function getAllByWhere($where = [], $files = '*')
    {
        $data = self::select($files, $where);
        $data = array_map(function ($_data){
            return self::_filter($_data);
        },$data);
        return $data;
    }

    public static function getOneByWhere($where = [], $files = '*')
    {
        $data = self::get($files, $where);
        return $data;
    }

    public static function addData($data)
    {
        try {
            self::insert($data);
            return self::id();
        } catch (Exception $e) {
            return false;
        }
    }

    public static function updateData($data, $where)
    {
        try {
            self::update($data, $where);
            return true;
        } catch (Exception $e) {
            return false;
        }
    }

    public static function _filter($data){
        foreach($data as $_k=>&$_v){
            switch ($_k){
                case 'img':
                    $data['img_url'] = !empty($_v) ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $_v, ImgUrl::IMAGE_SIZE_OP_ORI) : '';
                    break;
            }
        }
        return $data;
    }

}